* Table A8 Panel regressions of ex-post outcomes on productivity shock
* sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if ex_post_balp==1

tostring villageno, replace


*Generate shadow payout variables for control
*First, generate payout per policy for people who actually purchased insurance

replace payout_pp = payout_pp *1000
replace payout_pp = 0 if payout_pp==.

* Control weather station assignment by taluka
bys ws_c1 wave : egen payout_pp_all_c1 = max(payout_pp)
* Control weather station assignment by geography
bys ws_c2 wave : egen payout_pp_all_c2 = max(payout_pp)

* Only control farmers
keep if treat_year==0

* Creating table
local table = 1
local rastring "replace"
local outcomevars "w_real_cons w_real_food_exp w_real_events_exp w_real_gifts_out w_real_nonfood_exp w_real_durables_exp w_real_income w_real_gifts_in w_real_sav w_real_lent w_real_borrow"

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c1 year2*  firstt_2007 lost_w3 resurveyed , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c1 firstt_2007 lost_w3 resurveyed , fe cluster(villageno)
	estimates store cT`table'C`col++'

	qui estout cT`table'C* using "../output/a8a.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (payout_pp_all_c1) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

local table = 1
local rastring "replace"
local outcomevars "w_real_cons w_real_food_exp w_real_events_exp w_real_gifts_out w_real_nonfood_exp w_real_durables_exp w_real_income w_real_gifts_in w_real_sav w_real_lent w_real_borrow"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c2 year2*  firstt_2007 lost_w3 resurveyed if c_b2009!=1, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c2 firstt_2007 lost_w3 resurveyed if c_b2009!=1, fe cluster(villageno)
	estimates store cT`table'C`col++'

	qui estout cT`table'C* using "../output/a8b.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (payout_pp_all_c2) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

* Well-being proxies

replace payout_pp_all_c1 = payout_pp_all_c1/1000 // In K Rupees
replace payout_pp_all_c2 = payout_pp_all_c2/1000

* creating table
local table = 1
local outcomevars "food_sufficiency_child fladder_std outlook_std control_std"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c1 year2*  firstt_2007 lost_w3 resurveyed if c_b2009!=1, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c1 firstt_2007 lost_w3 resurveyed if c_b2009!=1, fe cluster(villageno)
	estimates store cT`table'C`col++'

	qui estout cT`table'C* using "../output/a8a-1.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (payout_pp_all_c1) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}
local table = 1
local outcomevars "food_sufficiency_child fladder_std outlook_std control_std"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c2 year2*  firstt_2007 lost_w3 resurveyed if c_b2009!=1, fe cluster(villageno)
	estimates store cT`table'C`col++'

	/* Balanced 2009 (Village IV sample) with Ind FE */
	xtreg `ovar' payout_pp_all_c2 firstt_2007 lost_w3 resurveyed if c_b2009!=1, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	qui estout cT`table'C* using "../output/a8b-1.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (payout_pp_all_c2) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}
